// @/utils/verificationDB.js
import Datastore from 'nedb';
import { verificationCodeFILE } from '../app/api/const'; // 根据实际路径调整

// 全局唯一实例，避免重复初始化
const verificationCodesDB = new Datastore({
  filename: verificationCodeFILE,
  autoload: true,
  timestampData: true
});

// 封装常用操作为 Promise 方法
export const verificationDB = {
  // 查找记录
  findOne: (query) => {
    return new Promise((resolve, reject) => {
      verificationCodesDB.findOne(query, (err, doc) => {
        if (err) reject(err);
        else resolve(doc);
      });
    });
  },
  // 插入记录
  insert: (doc) => {
    return new Promise((resolve, reject) => {
      verificationCodesDB.insert(doc, (err, newDoc) => {
        if (err) reject(err);
        else resolve(newDoc);
      });
    });
  },
  // 删除记录
  remove: (query, options = {}) => {
    return new Promise((resolve, reject) => {
      verificationCodesDB.remove(query, options, (err, numRemoved) => {
        if (err) reject(err);
        else resolve(numRemoved);
      });
    });
  }
};

export default verificationCodesDB;